$(document).ready(function(){
    initFenlei();
});

function initFenlei() {
    $.ajax({
        url: "/fenlei/findAll",
        type: 'GET',
        data: {},
        dataType: "JSON",
        async: false,
        success: function (res) {
            if(res.code === "200") {
                var data = res.data;
                var xlk = "";
                for (var i  = 0; i < data.length; i ++){
                    xlk += "<option class='chosena' value=" + data[i].id + ">" + data[i].name + "</option>";
                }

                $("#contenta").html(xlk);

                $("#contenta").trigger("chosen:updated");
            }else {
                alert(res.msg)
            }
        },
        error: function (e) {
            alert('发生错误')
        }

    })
}

function addCommodity(){
    // var data = $("input[type='checkbox'][name='supplierCheck']:checked");
    //
    // var commodityFenlei = [];
    // data.each(function () {
    //     commodityFenlei.push($(this).val())
    // });
    var fenleiids = $(".contenta").val();

    var name = $("input[name='name']").val();
    var price = $("input[name='price']").val();
    var stock = $("input[name='stock']").val()

    $.ajax({
        url: "/commodity/addCommodity",
        type: 'GET',
        data: {
            'name': name,
            'price': price,
            'stock': stock,
            'commodityFenlei': fenleiids
        },
        dataType: "JSON",
        traditional: true,
        async: false,
        success: function (res) {
            if(res.code == "201") {
                alert(res.msg);
            }
            window.location.href = "/commoditieView/" + res.data.id;
        },
        error: function (e) {
            alert('发生错误')
        }
    })

}